package abc.generator;

import abc.config.DatasourceConfigure;
import abc.config.MyBatisPlusAutoFillProperty;
import abc.config.MybatisPlusAutoFillConfig;
import abc.model.DSInfo;
import abc.utils.JacksonUtil;
import abc.utils.JwtUtil;
import abc.utils.StringUtil;
import abc.utils.YamlUtil;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FastAbcGenerator {
    public static void main(String[] args) throws FileNotFoundException {
        YamlUtil.loadConfig("application");
        Map<String, Object> datasourceMap = YamlUtil.getMap("spring.datasource.dynamic.datasource");
        Map<String, DSInfo> dsInfoMap = new HashMap<>();
        datasourceMap.entrySet().forEach(item-> {
            try {
                dsInfoMap.put(item.getKey(), JacksonUtil.convert(item.getValue(), DSInfo.class));
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
        DatasourceConfigure datasourceConfigure = new DatasourceConfigure();
        datasourceConfigure.setDatasource(dsInfoMap);

        MybatisPlusAutoFillConfig myBatisPlusAutoFillConfig = new MybatisPlusAutoFillConfig();
        Map<String, Object> autoFillMap = YamlUtil.getMap("mybatis-plus.global-config.db-config.auto-fill");
        if(autoFillMap != null) {
            try {
                myBatisPlusAutoFillConfig.setAutoFillProperty(JacksonUtil.convert(autoFillMap, MyBatisPlusAutoFillProperty.class));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if(args != null && args.length>0 && "mp".equals(args[0])) {
            MyBatisPlusGenerator generator = new MyBatisPlusGenerator(datasourceConfigure);
            generator.run();
        }
        else {
            String projectName = "";
            if(args.length>1 && StringUtil.isNotEmpty(args[1])) {
                projectName = args[1];
            }

            String supportNacos = "false";
            if(args.length>2 && StringUtil.isNotEmpty(args[2])) {
                supportNacos = args[2];
            }

            String context = "";
            if(args.length>3 && StringUtil.isNotEmpty(args[3])) {
                context = args[3];
            }

            List<String> excludeFields = new ArrayList<>();
            if(args.length>4 && StringUtil.isNotEmpty(args[4])) {
                excludeFields = StringUtil.splitString2List(args[4], ",");
            }

            List<String> logicDeleteField = new ArrayList<>();
            if(args.length>5 && StringUtil.isNotEmpty(args[5])) {
                logicDeleteField = StringUtil.splitString2List(args[5], ",");
            }

            List<String> versionField = new ArrayList<>();
            if(args.length>6 && StringUtil.isNotEmpty(args[6])) {
                versionField = StringUtil.splitString2List(args[6], ",");
            }

            List<String> ignoreAssociative = new ArrayList<>();
            if(args.length>7 && StringUtil.isNotEmpty(args[7])) {
                ignoreAssociative = StringUtil.splitString2List(args[7], ",");
            }

            String apiInputParameterType = "";
            if(args.length>8 && StringUtil.isNotEmpty(args[8])) {
                apiInputParameterType = args[8];
            }

            Boolean swagger = false;
            if(args.length>9 && StringUtil.isNotEmpty(args[9])) {
                swagger = "true".equals(args[9]);
            }

            String application = "service-abc";
            if(args.length>10 && StringUtil.isNotEmpty(args[10])) {
                application = args[10];
            }

            AbcGenerator generator = new AbcGenerator(datasourceConfigure, myBatisPlusAutoFillConfig, projectName, "true".equals(supportNacos.trim()), context, excludeFields, logicDeleteField, versionField, ignoreAssociative, apiInputParameterType, swagger, application);
            generator.run();
        }
    }
}
